package com.fozu.office.sale.web;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

/*
 *销售计划 
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fozu.common.config.Constants;
import com.fozu.common.pojo.ResultVO;
import com.fozu.common.util.IdGen;
import com.fozu.office.sale.entity.PlanVO;
import com.fozu.office.sale.service.PlanServiceImpl;
import com.fozu.office.system.entity.UserVO;
/*
 * 销售计划
 */
@Controller
@RequestMapping(value="plan")
public class PlanController {
	@Autowired
	private PlanServiceImpl planSve;
	
	//跳转到计划列表
	@RequestMapping(value="planList.shtml")
	public String toPlanList(){
		return "sale/plan/planList";
	}
	//跳转到修改页面
	@RequestMapping(value="planUpdate.shtml")
	public String toUpdatePlan(){
		return "sale/plan/planUpdate";
	}
	//跳转到添加页面
	@RequestMapping(value="planAdd.shtml")
	public String toAddPlan(){
		return "sale/plan/planAdd";
	}
	//跳转到审核页面
	@RequestMapping(value="planAudit.shtml")
	public String toAuditPlan(){
		return "sale/plan/planAudit";
	}
	//计划列表
	@PostMapping(value="getPlanList")
	@ResponseBody
	public ResultVO getPlanList(@RequestParam(value="pageNo") Long pageNo,
								@RequestParam(value="pageSize") Long pageSize,
								@RequestParam(value="month", required=false ) String month){
		try{
			PlanVO plan = new PlanVO();
			plan.setMonth(month);
			IPage <PlanVO> ipage = planSve.listPlan(new Page<PlanVO>(pageNo,pageSize), plan);
			return new ResultVO(Constants.status_SUCCESS,"获取成功",ipage);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"获取失败",null);	
		}
	}
	//删除
	@PostMapping(value="deletePlans")
	@ResponseBody
	public ResultVO deletePlans(@RequestParam(value="planIds") String planIds){
		try{
			String []ids = planIds.split(",");
			planSve.delPlans(ids);
			return new ResultVO("data");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"删除失败",null);
		}
	}
	
	//通过Id拿到对象
	@PostMapping(value="getPlanById")
	@ResponseBody
	public ResultVO getPlanById(@RequestParam(value="planId") String planId){
		try{
			PlanVO plan = new PlanVO();
			plan.setId(planId);
			PlanVO pl = planSve.getPlanById(plan);
			return new ResultVO(pl);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"获取失败",null);
		}
	}
	//修改
	@PostMapping(value="updatePlan")
	@ResponseBody
	public ResultVO updatePlan(HttpServletRequest request,
							   @RequestParam(value="id") String id,
							   @RequestParam(value="userName") String userName,
							   @RequestParam(value="month") String month,
							   @RequestParam(value="content") String content){
		try{
			PlanVO plan = new PlanVO(id,userName,month,content);
			UserVO loginUser = (UserVO) request.getSession().getAttribute("loginUser");
			plan.setUpdateDate(new Date());
			plan.setUpdateBy(loginUser.getId());
			planSve.updatePlan(plan);
			return new ResultVO(Constants.status_SUCCESS,"修改成功",null);
		}catch (Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"修改失败",null);
		}
	}
	//添加
	@PostMapping(value="doAddPlan")
	@ResponseBody
	public ResultVO addPlan(HttpServletRequest request,
							@RequestParam(value="userName" , required=true) String userName,
							@RequestParam(value="managerName",required=true) String managerName,
							@RequestParam(value="month",required=true) String month,
							@RequestParam(value="content",required=true) String content){
		try{
			String id= IdGen.getId();
			UserVO loginUser = (UserVO) request.getSession().getAttribute("loginUser");
			String createBy = loginUser.getId();
			PlanVO plan = new PlanVO(id,userName,managerName,month,content);
			plan.setCreateBy(createBy);
			plan.setCreateDate(new Date());
			plan.setUpdateDate(new Date());
			plan.setUpdateBy(createBy);
			planSve.addPlan(plan);
			return new ResultVO (Constants.status_SUCCESS,"添加成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"添加失败",null);
		}
	}
	//审核
	@PostMapping(value="updateStatus")
	@ResponseBody
	public ResultVO updateStatus(@RequestParam(value="status",required=true ) Integer status,
								 @RequestParam(value="id" ,required=true) String id){
		try{
			PlanVO plan =new PlanVO();
			plan.setStatus(status);
			plan.setId(id);
			planSve.updateStatus(plan);
			return new ResultVO(Constants.status_SUCCESS,"审核成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"审核失败",null);
		}
	}
}
